package com.learn.mvc.controller;

import com.learn.mvc.bean.User;
import org.springframework.http.RequestEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 请求报文转换
 * @author liangchao
 * @create 2021-10-19 22:25
 */
@Controller
public class HttpController {

    @RequestMapping("/testRequestBody")
    public String testRequestBody(@RequestBody String requestBody) {
        System.out.println("requestBody:" + requestBody); // requestBody:username=admin&password=123
        return "success";
    }

    @RequestMapping("/testRequestEntity")
    public String testRequestEntity(RequestEntity<String> requestEntity) {
        //当前requestEnity表示整个请求报文的信息
        // 泛型用string 标识将请求报文以字符串的方式获取请求报文
        System.out.println("请求头：" + requestEntity.getHeaders());
        System.out.println("请求体：" + requestEntity.getBody());
        // 请求头：[host:"localhost:8080", connection:"keep-alive", content-length:"27", cache-control:"max-age=0", sec-ch-ua:""Chromium";v="94", "Google Chrome";v="94", ";Not A Brand";v="99"", sec-ch-ua-mobile:"?0", sec-ch-ua-platform:""Windows"", upgrade-insecure-requests:"1", origin:"http://localhost:8080", user-agent:"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36", accept:"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9", sec-fetch-site:"same-origin", sec-fetch-mode:"navigate", sec-fetch-user:"?1", sec-fetch-dest:"document", referer:"http://localhost:8080/springmvc/toHttpMessage", accept-encoding:"gzip, deflate, br", accept-language:"zh-CN,zh;q=0.9,zh-TW;q=0.8,en;q=0.7,sq;q=0.6", cookie:"JSESSIONID=0C15D4F45C0F2D04D954978CB7194A0D; username=admin_itsm", Content-Type:"application/x-www-form-urlencoded;charset=UTF-8"]
        // 请求体：username=admin&password=123
        return "success";
    }

    // 原生servlet响应方法
    // 返回值如果是string，就变成表示要返回一个页面，进行页面跳转。
    // 但是现在我们不需要进行页面跳转，所以，返回值设置为void，或者return null;
    @RequestMapping("/testResponse")
    public void testResponse(HttpServletResponse response) throws IOException {
        // 用 write也行
//        response.getWriter().write("hello,response");
        // print 用来响应任意的数据
        // print 将括号中的内容，作为响应报文的响应体响应到浏览器
        // 为什么通过转发和重定向，我们就能看到一个页面？ 因为此时请求对应的响应的响应体，就是一个页面。
        response.getWriter().print("hello,response");
    }

    @RequestMapping(value = "/testResponseBody", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String testResponseBody() {
        // @ResponseBody 使得当前返回的字符串不再是视图名称，而是响应体
        return "成功";
    }

    // SpringMVC处理json对象
    @RequestMapping("/testResponseUser")
    @ResponseBody
    public User testResponseUser() {
        return new User(1001, "admin", "123456", 23, "男","xxx@163.com");
    }

    // SpringMVC处理ajax
    @RequestMapping("/testAxios")
    @ResponseBody
    public String testAxios(String username, String password) {
        System.out.println(username + "," + password);
        return "hello,axios";
    }
}
